#!/usr/bin/env python

import xml.parsers.expat
import time
import datetime
import calendar 
import re

def getTicketInfo(ggusDumpXML):
    info = {}

    def start_element(name, attrs):
        if name == 'ticket':

            print attrs

            id        = attrs['request_id']
            info[id]  = {} 
            info[id]['ctime']     = int( attrs['date_of_creation'] )
            info[id]['utime']     = int( attrs['last_update'] )
            info[id]['duration']  = info[id]['utime'] - info[id]['ctime'] 
            info[id]['status']    = attrs['status']
            info[id]['unit']      = attrs['responsible_unit']
            info[id]['type']      = attrs['ticket_type']
            info[id]['priority']  = int( attrs['priority_colour'] )

            try:
                info[id]['site']  = attrs['affected_site']
            except KeyError:
                info[id]['site']  = ''
        else:
            pass

    def end_element(name):
        pass
    
    def char_data(data):
        pass
    
    p = xml.parsers.expat.ParserCreate()
    
    p.StartElementHandler  = start_element
    p.EndElementHandler    = end_element
    p.CharacterDataHandler = char_data
    
    f = open(ggusDumpXML,'r')
    p.ParseFile(f)
    f.close()

    return info 

def mkRootTree(info, output):
    ## creating TTree
    from ROOT import TFile, TTree
    from array import array
    from ROOT import gROOT, AddressOf

    gROOT.ProcessLine(
    "struct GGUSInfoStruct {\
       Char_t  fqid[8];\
       Char_t  status[16];\
       Char_t  type[8];\
       Char_t  site[32];\
       Char_t  unit[32];\
    };" );

    from ROOT import GGUSInfoStruct

    ggusInfo = GGUSInfoStruct()

    ctime    = array('i',[0])
    utime    = array('i',[0])
    duration = array('i',[0])
    priority = array('i',[0])
    
    f = TFile(output,'RECREATE') 
    t = TTree('Tree_GGUSInfo','GGUS ticket information Tree')

    t.Branch('fqid'    , AddressOf(ggusInfo,'fqid')   , 'fqid/C')
    t.Branch('status'  , AddressOf(ggusInfo,'status') , 'status/C')
    t.Branch('type'    , AddressOf(ggusInfo,'type') , 'type/C')
    t.Branch('site'    , AddressOf(ggusInfo,'site') , 'site/C')
    t.Branch('unit'    , AddressOf(ggusInfo,'unit') , 'unit/C')
    t.Branch('ctime'   , ctime, 'ctime/I')
    t.Branch('utime'   , utime, 'utime/I')
    t.Branch('duration', duration, 'duration/I')
    t.Branch('priority', priority, 'priority/I')

    for id,d in info.items():

        ggusInfo.fqid   = id
        ggusInfo.status = d['status'] 
        ggusInfo.type   = d['type'] 
        ggusInfo.site   = d['site'] 
     
        ctime[0] = d['ctime'] 
        utime[0] = d['utime']
        duration[0] = d['duration']
        priority[0] = d['priority']
     
        t.Fill()

    t.Print()
    t.Show(0)

    f.Write()
    f.Close()

## get creation time and ticket info from 2 sources
info = getTicketInfo('ggus_tickets_atlas_2010Nov.xml')

## making ROOT file for further statistic studies
mkRootTree(info, 'ggus_tickets_atlas_2010Nov.root')
